package com.example.exercise;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.exercise.MysqlUtil.ClassOpt;
import com.example.exercise.MysqlUtil.DBService;

public class Del extends AppCompatActivity {

    EditText name,class1,class2;
    Button music,deluser,change,add,delclass,delwholeclass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_del);


        InitView();

        delwholeclass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (ClassOpt.delWholeClass(name.getText().toString()) == 1)
                        {
                            Looper.prepare();
                            Toast.makeText(getApplicationContext(),"删除"+name.getText().toString()+"所有学生成功",Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }else
                        {
                            Looper.prepare();
                            Toast.makeText(getApplicationContext(),"删除失败,可能网络问题或者找不到班级",Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }
                    }
                }).start();
            }
        });

        delclass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new  Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (ClassOpt.delClass(class1.getText().toString()) == 1)
                        {
                            Looper.prepare();
                            Toast.makeText(getApplicationContext(),"删除成功",Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }else
                        {
                            Looper.prepare();
                            Toast.makeText(getApplicationContext(),"删除失败,可能网络问题或者找不到班级",Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }
                    }
                }).start();
            }
        });
        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (ClassOpt.ChangeClass(class1.getText().toString(),class2.getText().toString()) == 1)
                        {
                            Looper.prepare();
                            Toast.makeText(getApplicationContext(),"修改成功",Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }else
                            {
                                Looper.prepare();
                                Toast.makeText(getApplicationContext(),"修改失败,可能网络问题或者找不到班级",Toast.LENGTH_SHORT).show();
                                Looper.loop();
                            }
                    }
                }).start();
            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                new  Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (DBService.insertClassData(class1.getText().toString()) == 1)
                        {
                            Looper.prepare();
                            Toast.makeText(getApplicationContext(),"增加成功",Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }else
                        {
                            Looper.prepare();
                            Toast.makeText(getApplicationContext(),"删除失败,可能网络问题",Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }
                    }
                }).start();
            }
        });

        music.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (name.getText().toString().equals(""))
                {
                    Toast.makeText(getApplicationContext(),"用户不能为空",Toast.LENGTH_SHORT).show();
                }
                else
                {
                   new Thread(new Runnable() {
                       @Override
                       public void run() {

                               if (DBService.getUser(name.getText().toString())==1){

                                   if (DBService.getUserMusic(name.getText().toString())==1)
                                   {
                                       Looper.prepare();
                                       showmusicDialog();
                                       Looper.loop();
                                   }else{
                                       Looper.prepare();
                                       shownomusicDialog();
                                       Looper.loop();

                                   }
                               }
                               else {
                                   Looper.prepare();
                                   Toast.makeText(getApplicationContext(),"用户不存在",Toast.LENGTH_SHORT).show();
                                   Looper.loop();
                               }



                       }
                   }) .start();
                }
            }
        });
    }

    private void InitView() {

        class1 = findViewById(R.id.beforeclass);
        class2 = findViewById(R.id.afterclass);
        change = findViewById(R.id.change);
        add = findViewById(R.id.add);
        name = findViewById(R.id.delname);
        music =  findViewById(R.id.ifmusic);
        deluser =  findViewById(R.id.deluser);
        delclass = findViewById(R.id.delclass);
        delwholeclass =findViewById(R.id.deluser2);
    }

    private void showmusicDialog() {

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(Del.this);
        normalDialog.setTitle("更改提示");
        normalDialog.setMessage("该学生是乐队，是否取消乐队");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (DBService.updatemusic(name.getText().toString()) !=-1)
                        {

                            Toast.makeText(getApplicationContext(),"更改成功",Toast.LENGTH_SHORT).show();

                        }else
                            {

                                Toast.makeText(getApplicationContext(),"更改失败",Toast.LENGTH_SHORT).show();

                            }
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do

                    }
                });
        // 显示
        normalDialog.show();
    }

    private void shownomusicDialog() {

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(Del.this);
        normalDialog.setTitle("更改提示");
        normalDialog.setMessage("该学生不是乐队，是否增加乐队");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (DBService.updatenomusic(name.getText().toString()) !=-1)
                        {

                            Toast.makeText(getApplicationContext(),"更改成功",Toast.LENGTH_SHORT).show();

                        }else
                        {

                            Toast.makeText(getApplicationContext(),"更改失败",Toast.LENGTH_SHORT).show();

                        }

                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        // 显示
        normalDialog.show();
    }
}